package com.yxkj.railservicer.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;
import com.yxkj.railservicer.util.SPUtil;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by sunyu on 2017/3/19.
 */

public class MyApplication extends Application {
    private static MyApplication instance;
    private static Context context;
    private final String APP_ID = "fa25ac9c1b";

    private List<Activity> list;	//activity：管理
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        context = getApplicationContext();

        list = new ArrayList<Activity>();
// 获取当前包名
        String packageName = context.getPackageName();
// 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
// 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
// 初始化Bugly
//        CrashReport.initCrashReport(context, APP_ID, false, strategy);
        Bugly.init(getApplicationContext(), APP_ID, false);

        //启动极光推送
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        String rid = JPushInterface.getRegistrationID(getApplicationContext());
        SPUtil.setString(context,"channelId",rid);
        //启动极光结束
    }
    /**
     * 获取进程号对应的进程名
     *
     * @param pid 进程号
     * @return 进程名
     */
    private static String getProcessName(int pid) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }
    public static MyApplication getInstance(){
        return instance;
    }
    /**
     * TODO	注册activity
     */
    public void addActivity(Activity a){
        list.add(a);
    }
    /**
     * 退出所有登记activity
     */
    public void exitAllActivity() {
        for (Activity a : list)a.finish();
    }

    public static Context getContext(){
        return context;
    }
}
